<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="international.jsp" %> 
<%@page import="com.brother.service.UserService,com.brother.service.TipService,com.brother.model.Customer"%>
<jsp:useBean id="customer" class="com.brother.model.Customer"></jsp:useBean>
<jsp:setProperty property="*" name="customer"/>
<%
java.util.Locale localeObj = request.getLocale();
String isoLanguage = localeObj.getISO3Language();
//  Portuguese 	Portuguese 	portugais 	por	pt
boolean isPortuguese = isoLanguage.equalsIgnoreCase("por") || isoLanguage.equalsIgnoreCase("pt") ;
if(isPortuguese){
	session.setAttribute("locale", "pt");
}
locale = (String)session.getAttribute("locale");
org.apache.log4j.Logger.getLogger(this.getClass()).debug("logining..."+request.getRemoteAddr());
com.brother.service.UserService s = new com.brother.service.UserService();
Customer loginUser = s.login(customer);
String action = request.getParameter("action");
boolean failLogin = false;
String redirectUrl = request.getParameter("redirectUrl")==null?"":request.getParameter("redirectUrl");
if(loginUser != null){
	loginUser.setTimeZoneOffSet(customer.getTimeZoneOffSet());
	session.setAttribute(com.brother.Constants.LOGIN_SESSTION_NAME, loginUser);
	response.sendRedirect(redirectUrl);
	TipService tps = new TipService();
	tps.addUserTrackerTips(loginUser.getUserid()+"");
	s.setUserLocale((String)session.getAttribute("locale"), loginUser.getUserid()); 
}else if("login".equalsIgnoreCase(action)){
	failLogin = true;
}

if(action != null && action.equalsIgnoreCase("logout")){
	session.removeAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
}




%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title><fmt:message key="user.login"/> - <fmt:message key="company.name"/></title>
<meta name="description" content="<fmt:message key="description.keyword"/>" />
<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<link rel="shortcut icon" href="servericon.gif"/>
    <script type="text/javascript" src="js/ext-base.js"></script>
    <script type="text/javascript" src="js/ext-all.js"></script>
<style>



</style>
<script>

if(top != self){
	var url = self.location.href;
	if(url.indexOf("?") > -1)
	url = url.substring(0, url.indexOf("?"));
	
	top.location.href = url;
}

						function SetCookie(name, value)
						{
						var expdate = new Date();
						var argv = SetCookie.arguments;
						var argc = SetCookie.arguments.length;
						var expires = (argc > 2) ? argv[2] : null;
						var path = (argc > 3) ? argv[3] : null;
						var domain = (argc > 4) ? argv[4] : null;
						var secure = (argc > 5) ? argv[5] : false;
						if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
						document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
						+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
						+((secure == true) ? "; secure" : "");
						}		
						
						SetCookie('language', '<%=locale%>', 24*60*60*1000*30);			
					</script>
					
<script>
function rememberUser(cbox){

  if(cbox.checked){
  	var name = document.getElementById("name").value;
	var password = document.getElementById("password").value;
	
  	SetCookie('name', name, 24*60*60*1000*30);
  	SetCookie('password', password, 24*60*60*1000*30);
	SetCookie('isRemember', 'true', 24*60*60*1000*30);

  }else{
    SetCookie('name', '', 1);
  	SetCookie('password', '', 1);
	SetCookie('isRemember', 'false', 24*60*60*1000*30);
  
  }
}

function setLoginInfo(){

	if(Ext.util.Cookies.get('isRemember') == 'true'){
		document.getElementById("name").value = Ext.util.Cookies.get('name');
		document.getElementById("password").value = Ext.util.Cookies.get('password');
		document.getElementById("rememberBox").checked = true;
	}
}

var pageType = "loginPage.jsp"; // Don't delete this for reloading page.

</script>
					
</head>
<body onload="setLoginInfo();">
<div id="msg" name="msg" style="background-color:#D4DF55; position:absolute; z-index:11; left: 10px; top: 61px; display:inline"></div>
<script>
       function Point(iX, iY){ 
          this.x = iX; 
          this.y = iY; 
       } 
 function fGetXY(obj){ 
           var oTmp = obj;     
           if(!oTmp||(typeof(oTmp.offsetLeft) == "undefined") 
               ||(typeof(oTmp.offsetTop) == "undefined") 
               ||!oTmp.tagName){ 
                return null; 
           } 
           var pt = new Point(0,0);   
           do{ 
      pt.x += oTmp.offsetLeft; 
      pt.y += oTmp.offsetTop; 
      //alert(oTmp.parentNode.tagName);
      oTmp = oTmp.parentNode; 
  } while(oTmp.tagName!="BODY"); 
  return pt; 
       } 
       
	   var gMsgTop = 0; //fGetXY(document.getElementById("tmp_pos")).y;
function show_tip(msg)
{
	if (gMsgTop == 0)
	{
		gMsgTop = fGetXY(document.getElementById("tmp_pos")).y + 5;
		document.getElementById("msg").style.top = gMsgTop;
		document.getElementById("msg").style.display = "inline";
		// alert(document.getElementById("msg").style.offsetTop);
	}
	
	document.getElementById("msg").innerHTML = msg;
}

function setTimeZone(){
	var date = new Date();
	var offSet = -date.getTimezoneOffset()/60;
	var offSetInput = document.getElementById("timeZoneOffSet");
	offSetInput.value = offSet;
	
}
</script>

<div class="gps_panel">

<div style="z-index:20;position:absolute;left:5%;top:20%"><img src="images/brazil_logo.jpg"/></div>

<div class="gps_login_panel_background"></div>
<div class="gps_login_panel">
<div class="gps_login_top"></div>
			<table width="100%" border="0" cellpadding="3" style="margin:0px 10px 10px 10px">
				<form action="login.jsp" method="post" onsubmit="rememberUser(document.getElementById('rememberBox'));">

				<tr>
					<td align="left" nowrap="nowrap"><fmt:message key="login.account"/></td>
					<td align="left"><fmt:message key="login.password"/></td>
				    <td align="left">&nbsp;</td>
				</tr>
				<tr>
					<td align="left">
				    <input type="text" name="name" id="name" value="" style="width:150px;border:0px;" /></td>
					<td align="left"><input type="password" name="password" id="password" value="" style="width:150px;border:0px;"></td>
				    <td align="left">
						<input type="hidden" name="redirectUrl" value="<%=redirectUrl%>" />
						<input type="hidden" id="timeZoneOffSet" name="timeZoneOffSet" value=""/> 
						<input type="hidden" name="action" value="login" />
						<input type="submit" name="submit" class="submit" onclick="setTimeZone();" value="<fmt:message key="user.login"/>" />	</td>
				</tr>
				<tr>
					<td align="left" colspan="3">
					<%locale = (String)session.getAttribute("locale"); %>
					
					<select style="<%=locale.equals("pt") ? "display:none":"" %>" id="language" name="language" onchange="SetCookie('language', this.value, 24*60*60*1000*30);window.location.reload();">
						<option value="en" <%if(locale.equals("en")){%>selected="selected"<%} %>>English</option>
						<option value="pt" <%if(locale.equals("pt")){%>selected="selected"<%} %>>Portugues</option>
						<option value="de" <%if(locale.equals("de")){%>selected="selected"<%} %>>German</option>
						<option value="ind" <%if(locale.equals("ind")){%>selected="selected"<%} %>>Indonesian</option>
						<option value="zh" <%if(locale.equals("zh")){%>selected="selected"<%} %>>中文</option>
					</select><%if(!locale.equals("pt")){%>&nbsp;&nbsp;<%} %>
					<input name="remember" id="rememberBox" onclick="rememberUser(this);" type="checkbox"  value="remember" />
<fmt:message key="login.remember"/></td>
			      </tr>
				
				<tr>
				<td  align="left" colspan="3"><%if(failLogin){%><span style="color:red"><fmt:message key="user.login.error"/></span><%} %></td>
				</tr>
				</form>
			</table>
	
			</div>

<div class="gps_footer">
<hr size=1 color="#a9bad3" style="width:100%">
<span style="color:#A0A0A4">Copyright &copy; 2012, All rights reserved. - <a href="mailto:">Contact us</a></span>
</div>
</div>


</body>
</html>
<%
com.brother.database.DataBase.closeThreadConnection();
%>